from html.parser import HTMLParser
import re
import sys
import pokeparser
import moveinfo

class move(HTMLParser):
	parser = pokeparser.pokeparser()

	def __init__(self):
		super().__init__()
		self.in_start = True
		self.in_table = self.in_td = self.in_ja = self.in_class = self.titlename = self.gettype = False
		self.mid = self.url = self.cname = self.jname = self.ename = self.types = self.classs = self.power = self.acc = self.pp = ''
		self.f = open('text/move.txt','a+',encoding='utf-8')
		self.u = open('url/moveurl.txt','a+',encoding='utf-8')
		self.move = []
		self.count = 0
		with open('text/move.txt',encoding='utf-8') as f:
			self.startcount = len(f.readlines())
		self.movetext = self.parser.gettext(self.parser.move())

	def handle_starttag(self, tag, attrs):
		attrs = dict(attrs)
		title = attrs.get('title','')
		if tag == 'tbody':
			self.in_table = True
		if tag == 'td' and self.in_table:
			self.in_td = True
			# 获得招式属性：
			cl = attrs.get('class', '')
			match = re.search('bg-(.+?) textwhite', cl)
			if match and self.gettype == False:
				self.types = match.group(1)
				self.gettype = True
		if tag == 'a' and self.mid != '' and self.in_table:
			# 获得招式中文名：
			if re.search('(.+?)（招式）', title):
				self.cname = re.search('(.+?)（招式）', title).group(1)
				self.url = self.parser.combine(attrs.get('href', ''))
			# 获得Z招式中文名：
			elif re.search('(.+?)Ｚ', title):
				self.titlename = True
			elif self.titlename:
				self.cname = title
				self.url = self.parser.combine(attrs.get('href', ''))
				self.titlename = False
			# 进入招式类别：
			elif re.search('(.+?)招式', title):
				self.in_class = True
			# 获得极巨招式中文名：
			elif re.search('(.*)极巨(.+)', title):
				self.cname = title
				self.url = self.parser.combine(attrs.get('href', ''))
		# 进入外文名：
		elif tag == 'td' and attrs.get('lang', '') == 'ja' and self.cname != '':
			self.in_ja = True

	def handle_data(self, data):
		data = str(data).strip('\n')
		# 获得招式id：
		if re.search('^[0-9]+$', data) and self.in_td and self.in_start:
			self.mid = '{num:03}'.format(num=int(data))
			self.in_start = False
		elif data == '—' and self.in_td and self.in_start:
			self.mid = '—'
			self.in_start = False
		# 获得外文名：
		elif self.in_ja:
			if self.jname == '':
				self.jname = data
			elif self.ename == '' and data != '':
				self.ename = data
				self.count += 1
				self.in_ja = False
		# 获得招式类别：
		elif self.in_class:
			self.classs = data
			self.in_class = False
		# 获得招式威力、命中、pp：
		elif re.search('^[0-9]+$', data) or data == '变化' or data == '—':
			if self.in_start == False and self.classs != '':
				if self.acc != '':
					self.setPP(data)
				elif self.power != '':
					self.acc = data
				else:
					self.power = data

	# 设置招式信息：
	def setPP(self, data):
		if self.count > self.startcount:
			m = moveinfo.moveinfo(self.url)
			m.feed(m.text)
			m.close()
			self.pp = data
			self.move = [self.mid, self.cname, self.jname, self.ename, self.types, self.classs, self.power, self.acc, self.pp, m.info, m.range, m.effect, m.pid]
			temp = sys.stdout
			sys.stdout = self.f
			print(self.move)
			self.f.flush()
			sys.stdout = temp
			print(self.move)
			sys.stdout = self.u
			print(self.url)
			self.u.flush()
			sys.stdout = temp
		self.in_start = True
		self.gettype = False
		self.mid = self.url = self.cname = self.jname = self.ename = self.types = self.classs = self.power = self.acc = self.pp = ''
		self.move = []

	def handle_endtag(self, tag):
		if tag == 'td':
			self.in_td = False
		if tag == 'tbody':
			self.in_table = False

def writetxt():
	m = move()
	m.feed(m.movetext)
	m.close()

if __name__ == "__main__":
	writetxt()